{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "588ebc0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f189abd-7493-4ed9-97a7-01a093674ce5",
   "metadata": {},
   "source": [
    "## 对DataFrame进行分组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "16f7bc9b",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.DataFrame({\n",
    "    '分店编号': ['001', '002', '001', '002', '001', '002', '001', '002'],\n",
    "    '时间段': ['2022Q1', '2022Q1', '2022Q1', '2022Q1', '2022Q2', '2022Q2', '2022Q2', '2022Q2'],\n",
    "    '商品类别': ['生鲜食品', '生鲜食品', '休闲食品', '休闲食品', '生鲜食品', '生鲜食品', '休闲食品', '休闲食品'],\n",
    "    '销售额': [1500, 2000, 3000, 2500, 1800, 2200, 3200, 2700],\n",
    "    '销售数量': [105,  84, 171, 162,  67, 150,  99,  57]\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "718721cb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th>商品类别</th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>1500</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>2000</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>3000</td>\n",
       "      <td>171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>2500</td>\n",
       "      <td>162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>1800</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>2200</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>3200</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>2700</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  分店编号     时间段  商品类别   销售额  销售数量\n",
       "0  001  2022Q1  生鲜食品  1500   105\n",
       "1  002  2022Q1  生鲜食品  2000    84\n",
       "2  001  2022Q1  休闲食品  3000   171\n",
       "3  002  2022Q1  休闲食品  2500   162\n",
       "4  001  2022Q2  生鲜食品  1800    67\n",
       "5  002  2022Q2  生鲜食品  2200   150\n",
       "6  001  2022Q2  休闲食品  3200    99\n",
       "7  002  2022Q2  休闲食品  2700    57"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "046f1db4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "分店编号\n",
       "001    2375.0\n",
       "002    2350.0\n",
       "Name: 销售额, dtype: float64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df针对分店编号进行分组，计算销售额平均值\n",
    "df.groupby(\"分店编号\")[\"销售额\"].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e6b4269b-14f5-4173-9daa-b09e6b25fe74",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>分店编号</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>001</th>\n",
       "      <td>2375.0</td>\n",
       "      <td>110.50</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>002</th>\n",
       "      <td>2350.0</td>\n",
       "      <td>113.25</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         销售额    销售数量\n",
       "分店编号                \n",
       "001   2375.0  110.50\n",
       "002   2350.0  113.25"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df针对分店编号进行分组，计算销售额和销售数量的平均值\n",
    "df.groupby(\"分店编号\", )[[\"销售额\", \"销售数量\"]].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "a849e09b-f3a1-449a-b24f-acb015ecbf97",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">001</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>2250.0</td>\n",
       "      <td>138.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>2500.0</td>\n",
       "      <td>83.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">002</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>2250.0</td>\n",
       "      <td>123.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>2450.0</td>\n",
       "      <td>103.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                销售额   销售数量\n",
       "分店编号 时间段                  \n",
       "001  2022Q1  2250.0  138.0\n",
       "     2022Q2  2500.0   83.0\n",
       "002  2022Q1  2250.0  123.0\n",
       "     2022Q2  2450.0  103.5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df针对分店编号和时间段进行分组，计算销售额和销售数量的平均值\n",
    "df.groupby([\"分店编号\", \"时间段\"])[[\"销售额\", \"销售数量\"]].mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "9c91a524",
   "metadata": {},
   "outputs": [],
   "source": [
    "def max_plus_10(nums):\n",
    "    return nums.max() + 10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6610de02-283b-4b2b-b3dd-e66978ce7663",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">001</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>3010</td>\n",
       "      <td>181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>3210</td>\n",
       "      <td>109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">002</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>2510</td>\n",
       "      <td>172</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>2710</td>\n",
       "      <td>160</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              销售额  销售数量\n",
       "分店编号 时间段               \n",
       "001  2022Q1  3010   181\n",
       "     2022Q2  3210   109\n",
       "002  2022Q1  2510   172\n",
       "     2022Q2  2710   160"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df针对分店编号和时间段进行分组，对销售额和销售数量进行max_plus_10操作\n",
    "df.groupby([\"分店编号\", \"时间段\"])[[\"销售额\", \"销售数量\"]].apply(max_plus_10)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fcfa54c5-7765-4643-b5f7-6229437861fb",
   "metadata": {},
   "source": [
    "## 使用透视表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cceea602-82df-4d37-b861-4f11430131dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th>商品类别</th>\n",
       "      <th>销售额</th>\n",
       "      <th>销售数量</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>1500</td>\n",
       "      <td>105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>2000</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>3000</td>\n",
       "      <td>171</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q1</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>2500</td>\n",
       "      <td>162</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>1800</td>\n",
       "      <td>67</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>生鲜食品</td>\n",
       "      <td>2200</td>\n",
       "      <td>150</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>001</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>3200</td>\n",
       "      <td>99</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>002</td>\n",
       "      <td>2022Q2</td>\n",
       "      <td>休闲食品</td>\n",
       "      <td>2700</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  分店编号     时间段  商品类别   销售额  销售数量\n",
       "0  001  2022Q1  生鲜食品  1500   105\n",
       "1  002  2022Q1  生鲜食品  2000    84\n",
       "2  001  2022Q1  休闲食品  3000   171\n",
       "3  002  2022Q1  休闲食品  2500   162\n",
       "4  001  2022Q2  生鲜食品  1800    67\n",
       "5  002  2022Q2  生鲜食品  2200   150\n",
       "6  001  2022Q2  休闲食品  3200    99\n",
       "7  002  2022Q2  休闲食品  2700    57"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d16d9134-e2d1-47ce-93a0-73173b1e0bd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>商品类别</th>\n",
       "      <th>休闲食品</th>\n",
       "      <th>生鲜食品</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">001</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>3000</td>\n",
       "      <td>1500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>3200</td>\n",
       "      <td>1800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">002</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>2500</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>2700</td>\n",
       "      <td>2200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "商品类别         休闲食品  生鲜食品\n",
       "分店编号 时间段               \n",
       "001  2022Q1  3000  1500\n",
       "     2022Q2  3200  1800\n",
       "002  2022Q1  2500  2000\n",
       "     2022Q2  2700  2200"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df的分店编号和时间段作为索引，商品类别作为列，计算销售额的总和\n",
    "pd.pivot_table(df, index=[\"分店编号\", \"时间段\"], columns=\"商品类别\", values=\"销售额\", aggfunc=np.sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "39eb936a-8797-4bb8-bf93-34c526d3d7e3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>商品类别</th>\n",
       "      <th>休闲食品</th>\n",
       "      <th>生鲜食品</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>分店编号</th>\n",
       "      <th>时间段</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">001</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>3000</td>\n",
       "      <td>1500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>3200</td>\n",
       "      <td>1800</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">002</th>\n",
       "      <th>2022Q1</th>\n",
       "      <td>2500</td>\n",
       "      <td>2000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022Q2</th>\n",
       "      <td>2700</td>\n",
       "      <td>2200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "商品类别         休闲食品  生鲜食品\n",
       "分店编号 时间段               \n",
       "001  2022Q1  3000  1500\n",
       "     2022Q2  3200  1800\n",
       "002  2022Q1  2500  2000\n",
       "     2022Q2  2700  2200"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df的分店编号和时间段作为索引，商品类别作为列，计算销售额的平均值\n",
    "pd.pivot_table(df, index=[\"分店编号\", \"时间段\"], columns=\"商品类别\", values=\"销售额\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "059bc55e-bec5-4946-833a-f360a2ac54ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>分店编号</th>\n",
       "      <th>001</th>\n",
       "      <th>002</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品类别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>休闲食品</th>\n",
       "      <td>3100</td>\n",
       "      <td>2600</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>生鲜食品</th>\n",
       "      <td>1650</td>\n",
       "      <td>2100</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "分店编号   001   002\n",
       "商品类别            \n",
       "休闲食品  3100  2600\n",
       "生鲜食品  1650  2100"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df的商品类别作为索引，分店编号作为列，计算销售额的平均值\n",
    "pd.pivot_table(df, index=\"商品类别\", columns=\"分店编号\", values=\"销售额\", aggfunc=np.mean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "9fd3364b-67ec-4a13-b638-83ec41e1a464",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>分店编号</th>\n",
       "      <th>001</th>\n",
       "      <th>002</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>商品类别</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>休闲食品</th>\n",
       "      <td>6200</td>\n",
       "      <td>5200</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>生鲜食品</th>\n",
       "      <td>3300</td>\n",
       "      <td>4200</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "分店编号   001   002\n",
       "商品类别            \n",
       "休闲食品  6200  5200\n",
       "生鲜食品  3300  4200"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 把df的商品类别作为索引，分店编号作为列，计算销售额的总和\n",
    "pd.pivot_table(df, index=\"商品类别\", columns=\"分店编号\", values=\"销售额\", aggfunc=np.sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4c2203c9-8d4b-4bfa-884d-0691ce172a78",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab7c3a07-c974-4a50-bfa7-ea2605ea0f92",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
